#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <iostream>

int cnt = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *count(void *num)
{
    uint64_t i = (uint64_t)num;
    while (true)
    {
        pthread_mutex_lock(&lock);
        pthread_cond_wait(&cond, &lock);
        printf("线程 %d cnt:%d\n", i, cnt++);
        pthread_mutex_unlock(&lock);
    }
}

int main()
{
    //64位整形，强转指针
    for (uint64_t i = 0; i < 5; i++)
    {
        pthread_t tid;
        //不能取i的地址传入,线程里访问的和这里的i会是同一个变量,
        //一个修改,另一个读取错误
        pthread_create(&tid, nullptr, count, (void*)i);
        //usleep(13);
    }

    sleep(3);
    while (true)
    {
        pthread_cond_signal(&cond);
        //pthread_cond_broadcast(&cond);
        //printf("唤醒\n");
        //usleep(10);
    }

    return 0;
}